package com.qzxy.premiumhotelsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzxy.premiumhotelsystem.common.constant.RedisKeyConstant;
import com.qzxy.premiumhotelsystem.common.constant.RoomStatusConstant;
import com.qzxy.premiumhotelsystem.common.result.PageResult;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.domain.dto.RoomStockPageDTO;
import com.qzxy.premiumhotelsystem.domain.dto.RoomTypesDTO;
import com.qzxy.premiumhotelsystem.domain.entity.RoomTypes;
import com.qzxy.premiumhotelsystem.domain.entity.Rooms;
import com.qzxy.premiumhotelsystem.domain.vo.RoomTypeCount;
import com.qzxy.premiumhotelsystem.domain.vo.RoomTypeDetailVO;
import com.qzxy.premiumhotelsystem.mapper.RoomTypesMapper;
import com.qzxy.premiumhotelsystem.service.RoomTypesService;
import com.qzxy.premiumhotelsystem.utils.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 14912
* @description 针对表【room_types】的数据库操作Service实现
* @createDate 2024-09-12 15:55:14
*/
@Service
public class RoomTypesServiceImpl extends ServiceImpl<RoomTypesMapper, RoomTypes>
    implements RoomTypesService{

    @Resource
    private RoomTypesMapper roomTypesMapper;

    @Resource
    private RedisUtil redisUtil;


    @Override
    public Result addRoomTypes(RoomTypesDTO roomTypesDTO) {
        try {
            roomTypesMapper.insert(BeanUtil.toBean(roomTypesDTO, RoomTypes.class));
        }catch (DuplicateKeyException e){
            throw new RuntimeException("房间类型唯一索引冲突");
        }
        return  Result.ok("房间类型新增成功");
    }

    @Override
    public void armoryRoomTypesMes() {

        List<RoomTypes> roomTypes = roomTypesMapper.selectList(null);
        redisUtil.setCacheObject(RedisKeyConstant.ROOM_TYPES_KEY,roomTypes);
    }

    @Override
    public Result getRoomTypeList() {

        List<RoomTypes> cacheList = redisUtil.getCacheObject(RedisKeyConstant.ROOM_TYPES_KEY);
        //以防管理员没装配
        if (CollUtil.isEmpty(cacheList)){
            cacheList = roomTypesMapper.selectList(null);
        }
        return Result.ok(cacheList);
    }


    @Override
    public Result selectByRoomTypeId(Integer roomTypeId) {
        RoomTypeDetailVO roomTypeDetailVO = roomTypesMapper.selectByRoomTypeId(roomTypeId);
        if (roomTypeDetailVO.getStock() == 0){
            List<RoomTypeDetailVO> roomTypeDetailVOList = roomTypesMapper.selectByRoomTypeIdButNOStock(roomTypeId);
            return Result.ok(roomTypeDetailVOList.get(0));
        }
        return Result.ok(roomTypeDetailVO);
    }

    @Override
    public Result selectAdviceRoomType(Integer isAdvice) {

        List<RoomTypeDetailVO> roomTypes = roomTypesMapper.selectAdviceRoomType(isAdvice);
        return Result.ok(roomTypes);
    }

    @Override
    public Result<PageResult> queryRoomStock(RoomStockPageDTO roomStockPageDTO) {

        IPage<RoomTypeDetailVO> page = new Page<>(roomStockPageDTO.getPageNo(),roomStockPageDTO.getPageSize());
        roomTypesMapper.queryRoomStock(page,roomStockPageDTO);
        return Result.ok(new PageResult(page.getTotal(),page.getRecords()));
    }

    @Override
    public Result countRooms() {
        List<RoomTypeCount> list = roomTypesMapper.countRooms();
        return Result.ok(list);
    }




}




